package com.example.product.converter;

import com.example.common.Interface.product.entity.vo.ProductVO;
import com.example.product.dao.po.Product;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class ProductConverter {
    public static Product toProduct(ProductVO vo) {
        Date date = new Date();
        return Product.builder()
                .id(vo.getId())
                .name(vo.getName())
                .description(vo.getDescription())
                .image(vo.getImage())
                .categories(listToString(vo.getCategories()))
                .brand(vo.getBrand())
                .sold(vo.getSold())
                .status(vo.getStatus())
                .createTime(date)
                .updateTime(date)
                .build();
    }

    public static ProductVO toProductVO(Product product, Long price) {
        return ProductVO.builder()
                .id(product.getId())
                .name(product.getName())
                .description(product.getDescription())
                .price(price)
                .image(product.getImage())
                .categories(stringToLongList(product.getCategories()))
                .brand(product.getBrand())
                .sold(product.getSold())
                .status(product.getStatus())
                .build();
    }

    public static String listToString(List<String> list) {
        return list != null
                ? list.stream().collect(Collectors.joining(","))
                : "";
    }

    // 新增字符串转列表方法（可放在类底部）
    public static List<String> stringToLongList(String str) {
        if (str == null || str.isEmpty()) return Collections.emptyList();
        return Arrays.stream(str.split(",")).collect(Collectors.toList());
    }
}
